<?php
	session_start();
	
	//LIBARY INCLUDES
	
	include "game/lib/functions.php";
	include "game/lib/class.ConfigManager.php";
	include "game/lib/class.DataRowManager.php";
	include "game/lib/class.DataListManager.php";
	include "game/lib/class.Template.php";
	
	
	//LIBARY INCLUDES ---  VÉGE
	
	

	try{
		$mainconf = new ConfigManager("config.cfg");
		
		if($mainconf->get("debugmode")){
			set_exception_handler('debug_error');
		}
		else{
			set_exception_handler('error');
		}
	}
	catch(Exception $e){
		die('Végzetes hiba!');
	}
	mysql_connect($mainconf->get("sqlserver"), $mainconf->get("sqluser"), $mainconf->get("sqlpass"));
	mysql_select_db($mainconf->get("sqldb"));
	mysql_query('SET NAMES "utf8"');

	
	
	
	
	// page érték
	if(isset($_GET['page'])){
		$_SESSION['page']=$_GET['page'];
		//refresh(); // nem szükséges, és lassítja a szervert
	}
	try{
		$pagename=$mainconf->get("id".$_SESSION['page']);
	} catch(InvalidArgumentException $e){
		$_SESSION['page']=$mainconf->get("defaultpageid");
		$pagename=$mainconf->get("id".$_SESSION['page']);
	}
	
	
	
	
	//language
	$langid=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // a felhasználó nyelvét jelölő 2 karakter
	try{
		$languagefile = $mainconf->get('language'.$langid); //ha létezik a felhasználó nyelvén
	}
	catch(InvalidArgumentException $e){
		$languagefile =  $mainconf->get('language'.$mainconf->get('defaultlangid')); //alapért. nyelv
	}
	
	//lang
	$lang = new ConfigManager("language/$languagefile.cfg");
	
	//style
	$style='default';
	
	$template = new Template("template/$style/template.".$pagename.".php");
	function setContents($c, $value){global $template;$template->setContent($c, $value);} // it's an alias only
	(include "language/function.".$languagefile.".php") or ex_throw( new Exception("File ("."language/function.".$languagefile.".php".") doesn't exists"));
	(include "template/processor.php") or ex_throw( new Exception("File (template/processor.php) doesn't exists"));
	(include "template/processor.".$pagename.".php") or ex_throw( new Exception("File ("."template/processor.".$pagename.".php".") doesn't exists"));
	
	
	$_output['version']=$mainconf->get("version");
	
	
	
	$template->setOutputs($_output);

	$template->setStrings("language/$languagefile.cfg");
	
	$template->flush();
	
?>
